package com.bjzc.AndroidSDK;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

import org.apache.http.util.EncodingUtils;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.DisplayMetrics;

public class Tools {
    public static int getHardVersion() {
        int kernelVersion = 0;
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("/proc/version");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return kernelVersion;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 8 * 1024);
        String info = "";
        String line = "";

        try {
            while ((line = bufferedReader.readLine()) != null) {
                info += line;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            if (info != "") {//Linux version 3.0.15 (root@wudw-bjzc) ( ) #ZC3700 SMP PREEMPT Thu Sep 4 16:28:14
                final String keyword = "#";
                int index = info.indexOf(keyword) + 2;
                line = info.substring(index + keyword.length());
                index = line.indexOf(" ");
                kernelVersion = Integer.parseInt(line.substring(0, index));
            }
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        return kernelVersion;
    }

    public static void writeFileData(Context mContext, String fileName, int data) {
        try {
            FileOutputStream fout = mContext.openFileOutput(fileName, android.content.Context.MODE_PRIVATE);
            byte[] bytes = String.valueOf(data).getBytes();
            fout.write(bytes);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeFileData(Context mContext, String fileName, String data) {
        try {
            FileOutputStream fout = mContext.openFileOutput(fileName, android.content.Context.MODE_PRIVATE);
            byte[] bytes = data.getBytes();
            fout.write(bytes);
            fout.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static int readFileData(Context mContext, String fileName) {
        int res = 1;
        try {
            FileInputStream fin = mContext.openFileInput(fileName);
            int length = fin.available();
            byte[] buffer = new byte[length];
            fin.read(buffer);
            String s = EncodingUtils.getString(buffer, "UTF-8");
            res = Integer.parseInt(s);
            fin.close();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return res;
    }

    public static String readFileDataS(Context mContext, String fileName) {
        String res = "";
        try {
            FileInputStream fin = mContext.openFileInput(fileName);
            int length = fin.available();
            byte[] buffer = new byte[length];
            fin.read(buffer);
            res = EncodingUtils.getString(buffer, "UTF-8");
            fin.close();
        } catch (Exception e) {
            //e.printStackTrace();
        }
        return res;
    }

    public static float get_adjust_ratio(Context mContext) {
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;
        float ratioWidth = (float) screenWidth / 320;
        float ratioHeight = (float) screenHeight / 480;

        float RATIO = Math.min(ratioWidth, ratioHeight);
        int OFFSET_LEFT, OFFSET_TOP;//调整的距离
        if (ratioWidth != ratioHeight) {
            if (RATIO == ratioWidth) {
                OFFSET_LEFT = 0;
                OFFSET_TOP = Math.round((screenHeight - 480 * RATIO) / 2);
            } else {
                OFFSET_LEFT = Math.round((screenWidth - 320 * RATIO) / 2);
                OFFSET_TOP = 0;
            }
        }
        return RATIO;
    }

    public static int getFontHeight(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        FontMetrics fm = paint.getFontMetrics();
        return (int) Math.ceil(fm.descent - fm.ascent);//Math.ceil(fm.descent - fm.top) + 2;
    }

    public static String updatefsl(String fsl, int tag) {
        String res = "";
        try {
            double source = Float.parseFloat(fsl);
            if (tag == 0) {
                source = source - 0.01;
            }
            if (tag == 1) {
                source = source + 0.01;
            }
            if (source <= 0.1) source = 0.1;
            if (source >= 1.0) source = 1.0;
            DecimalFormat df = new DecimalFormat("0.00");
            res = df.format(source);
        } catch (Exception e) {
            res = "0.97";
        }
        return res;
    }

}
